package com.truckdispatching.http.controller;

import java.util.HashMap;

import org.json.JSONObject;

import com.truckdispatching.UserManager;
import com.truckdispatching.http.util.TDHttpController;
import com.truckdispatching.util.ProvinceCityUtil;
import com.truckdispatching.util.TruckLengthUtil;

public class RegisterHttpController extends TDHttpController {

	@Override
	public String getRequestUrl() {
		return mHostUrl + "/wap/register.do";
	}

	public void sendRequest(String name, String mobile, String carNumber, String carLength, String target1,
			String target2, String target3, String imei) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("memberBO.USRREALNAME", name);
		params.put("memberBO.USRMOBILE", mobile);
		params.put("TKPLATENO", carNumber);
		params.put("DCT_TKLEN", TruckLengthUtil.getTruckLengthCode(carLength));
		params.put("TKTarget1", ProvinceCityUtil.getCodeByPcName(target1));
		params.put("TKTarget2", ProvinceCityUtil.getCodeByPcName(target2));
		params.put("TKTarget3", ProvinceCityUtil.getCodeByPcName(target3));
		params.put("imei", imei);
		sendRequest(params, true);
	}

	@Override
	public void onSuccess(String response) {
		try {
			JSONObject jo = getJSONObjectFromJSON(new JSONObject(response), "data", null);
			String token = getStringFromJSON(jo, "token", "");
			UserManager.setToken(token);
			UserManager.setIsLogin(true);
			callOnResponseListener(token);
		} catch (Exception e) {
			callOnResponseErrorListener(-1, "JSON数据异常");
		}
	}

	@Override
	public void onError(int errorCode, String errorMessage) {
		callOnResponseErrorListener(errorCode, errorMessage);
	}

}
